package com.hengpeng.itfin.service.impl;

import java.util.Date;

import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.hengpeng.common.exception.ServiceResult;
import com.hengpeng.common.util.MoneyUtil;
import com.hengpeng.itfin.service.TransferService;
import com.hengpeng.itfinbase.constants.RespCodeConstants;
import com.hengpeng.itfinbase.en.AccountType;
import com.hengpeng.itfinbase.en.TradeMode;
import com.hengpeng.itfinbase.en.TransType;
import com.hengpeng.itfinbase.message.partner.TransferRequest;
import com.hengpeng.itfinbase.message.partner.TransferResponse;
import com.hengpeng.itfinbase.persist.Transfer;
import com.hengpeng.itfinbase.persist.Transfer.TransferStatus;

/**
 * <pre>
 * 支付系统服务实现
 * </pre>
 * 
 * @author zhaojitao
 * @version 1.0, 2015-6-4
 */
@Service
@Lazy
@Transactional
public class TransferServiceImpl extends BaseServiceImpl implements TransferService {

    @Override
    public void packTransferRequest(TransferRequest request) {
        Transfer transfer = new Transfer();
        transfer.setMoney(MoneyUtil.convertLongYuanToFen(Double.parseDouble(request.getTransAmt())));
        transfer.setStatus(TransferStatus.DEALING);

        // 出账只能是平台账户，故暂时不计冻结流水
        transfer.setOutType(request.getOutType());
        transfer.setOutUserId(request.getOutUserId());
        transfer.setOutTrustAccountId(request.getOutCustId());
        transfer.setOutSubAccountId(request.getOutAcctId());

        transfer.setInType(request.getInType());
        transfer.setInUserId(request.getInUserId());
        transfer.setInTrustAccountId(request.getInCustId());
        transfer.setInSubAccountId(request.getInAcctId());
        transfer.setReferenceId(request.getReferenceId());
        transfer.setCreateTime(new Date());
        transfer.setUpdateTime(new Date());
        transfer = serviceFactory.getDaoFactory().getCommonDao().saveEntity(transfer);
        if (transfer.getId() == null) {
            throw new RuntimeException("转账记录保存出错!");
        } else {
            request.setOrdId(transfer.getId().toString());
        }

    }

    @Override
    public ServiceResult<Transfer> dealTransferResult(TransferResponse msg) {
        ServiceResult<Transfer> result = new ServiceResult<Transfer>();
        Transfer transfer = serviceFactory.getDaoFactory().getCommonDao()
                .getEntity(Transfer.class, Long.parseLong(msg.getOrdId()));
        transfer = daoFactory.getCommonDao().getEntity(Transfer.class, transfer.getId());
        if (transfer != null) {
            if (msg.getRespCode().equals(RespCodeConstants.CommonCode.TRANS_SUC)) {// 成功
                if (transfer.getStatus() == TransferStatus.DEALING) {
                    int i = serviceFactory
                            .getDaoFactory()
                            .getTransferDao()
                            .updateStatus(transfer.getId(), TransferStatus.SUCCESS, TransferStatus.DEALING,
                                    msg.getRespCode() + ":" + msg.getRespDesc(), new Date());
                    if (i > 0) {
                        if (transfer.getInType() == AccountType.PERSONAL) {// 只操作个人用户，平台用户暂不做本地资金操作
                            TransType transType = null;
                            if ("REDPACKETBACK".equals(msg.getMerPriv())) {// 红包划账
                                transType = TransType.INVEST_REDPACKET_BACK;
                            } else if ("REBATE".equals(msg.getMerPriv())) {// 返利划账
                                transType = TransType.INVEST_REBATE;
                            } else if ("ADDREBATE".equals(msg.getMerPriv())) {// 加息划账
                                transType = TransType.VIP_ADDRATE;
                            } else {// 普通划账
                                transType = TransType.TRANSFER;
                            }
                            serviceFactory.getAccountService().changeAccountMoney(transfer.getInUserId(),
                                    transfer.getMoney(), 0l, 0l, 0l, transType, TradeMode.ADD,
                                    String.valueOf(transfer.getId()), "转账成功");

                            // changeAccountMoney(transfer.getInUserId(),
                            // transfer.getMoney(), transType, TradeMode.ADD,
                            // String.valueOf(transfer.getId()),
                            // "转账成功");
                        }
                    }
                }
                result.setIsSuccess(true);
                result.setMessage(RespCodeConstants.CommonCode.TRANS_SUC_MSG);
                result.setCode(RespCodeConstants.CommonCode.TRANS_SUC);
                return result;
            } else {// 失败
                serviceFactory
                        .getDaoFactory()
                        .getTransferDao()
                        .updateStatus(transfer.getId(), TransferStatus.FAILED, TransferStatus.DEALING, "转账失败!",
                                new Date());
                result.setIsSuccess(false);
                result.setMessage(RespCodeConstants.CommonCode.TRANS_FAIL_MSG);
                result.setCode(RespCodeConstants.CommonCode.TRANS_FAIL);
                return result;
            }
        } else {
            // 未找到对账记录
            result.setIsSuccess(false);
            result.setMessage(RespCodeConstants.CommonCode.TRANS_FAIL_MSG);
            result.setCode(RespCodeConstants.CommonCode.DATA_NOT_FOUND);
            return result;
        }
    }
}